/**
 * Utility for generating test images of various sizes for performance testing
 */

/**
 * Generate a canvas with random colored pixels
 * @param width Canvas width
 * @param height Canvas height
 * @returns Canvas element
 */
export function generateCanvas(width: number, height: number): HTMLCanvasElement {
  const canvas = document.createElement('canvas');
  canvas.width = width;
  canvas.height = height;
  
  const ctx = canvas.getContext('2d');
  if (!ctx) {
    throw new Error('Failed to get canvas context');
  }
  
  // Fill with a random color
  ctx.fillStyle = getRandomColor();
  ctx.fillRect(0, 0, width, height);
  
  // Add some random shapes for variety
  for (let i = 0; i < 10; i++) {
    ctx.fillStyle = getRandomColor();
    const x = Math.random() * width;
    const y = Math.random() * height;
    const size = Math.random() * Math.min(width, height) * 0.2;
    ctx.fillRect(x, y, size, size);
  }
  
  return canvas;
}

/**
 * Generate a test image file from a canvas
 * @param canvas Canvas element
 * @param format Image format ('image/jpeg', 'image/png', 'image/webp')
 * @param quality Image quality (0-1)
 * @param filename Filename
 * @returns Promise with File object
 */
export async function canvasToFile(
  canvas: HTMLCanvasElement,
  format: string = 'image/jpeg',
  quality: number = 0.9,
  filename: string = 'test-image.jpg'
): Promise<File> {
  return new Promise<File>((resolve, reject) => {
    canvas.toBlob(
      (blob) => {
        if (blob) {
          const file = new File([blob], filename, { type: format });
          resolve(file);
        } else {
          reject(new Error('Failed to convert canvas to blob'));
        }
      },
      format,
      quality
    );
  });
}

/**
 * Generate multiple test images of various sizes
 * @param count Number of images to generate
 * @param minWidth Minimum image width
 * @param maxWidth Maximum image width
 * @param minHeight Minimum image height
 * @param maxHeight Maximum image height
 * @param format Image format ('image/jpeg', 'image/png', 'image/webp')
 * @returns Promise with array of File objects
 */
export async function generateTestImages(
  count: number,
  minWidth: number = 500,
  maxWidth: number = 2000,
  minHeight: number = 500,
  maxHeight: number = 2000,
  format: string = 'image/jpeg'
): Promise<File[]> {
  const files: File[] = [];
  
  for (let i = 0; i < count; i++) {
    const width = Math.floor(Math.random() * (maxWidth - minWidth + 1)) + minWidth;
    const height = Math.floor(Math.random() * (maxHeight - minHeight + 1)) + minHeight;
    
    const canvas = generateCanvas(width, height);
    const extension = format === 'image/jpeg' ? 'jpg' : format === 'image/png' ? 'png' : 'webp';
    const file = await canvasToFile(canvas, format, 0.9, `test-image-${i + 1}.${extension}`);
    
    files.push(file);
  }
  
  return files;
}

/**
 * Generate a random color
 * @returns Random color in hex format
 */
function getRandomColor(): string {
  const letters = '0123456789ABCDEF';
  let color = '#';
  for (let i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
  }
  return color;
}

/**
 * Generate test images of specific sizes
 * @param sizes Array of image sizes [width, height]
 * @param format Image format ('image/jpeg', 'image/png', 'image/webp')
 * @returns Promise with array of File objects
 */
export async function generateTestImagesWithSizes(
  sizes: Array<[number, number]>,
  format: string = 'image/jpeg'
): Promise<File[]> {
  const files: File[] = [];
  
  for (let i = 0; i < sizes.length; i++) {
    const [width, height] = sizes[i];
    
    const canvas = generateCanvas(width, height);
    const extension = format === 'image/jpeg' ? 'jpg' : format === 'image/png' ? 'png' : 'webp';
    const file = await canvasToFile(canvas, format, 0.9, `test-image-${width}x${height}.${extension}`);
    
    files.push(file);
  }
  
  return files;
}

/**
 * Generate a large test image
 * @param width Image width
 * @param height Image height
 * @param format Image format ('image/jpeg', 'image/png', 'image/webp')
 * @returns Promise with File object
 */
export async function generateLargeTestImage(
  width: number,
  height: number,
  format: string = 'image/jpeg'
): Promise<File> {
  const canvas = generateCanvas(width, height);
  const extension = format === 'image/jpeg' ? 'jpg' : format === 'image/png' ? 'png' : 'webp';
  return await canvasToFile(canvas, format, 0.9, `large-test-image-${width}x${height}.${extension}`);
}